In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.
Consider a chessboard of size . Some of its squares are marked and called forbidden squares. An ant is going to visit all squares of the chessboard except the forbidden ones. Each of the squares has to be visited exactly once. The tour begins in the start square, which is the top-left square of the board and has to finish in one of the periphery squares (the ant want to leave the board in the end). We assume that the start square is not forbidden. In a single step the ant can move to one of at most four neighbouring squares (i.e. she can move up, down, left or right).
The walk of our ant is recursive in a sense: to round the square of size the ant splits it into four parts (quarters) of size and then rounds each of them. It means if the ant enters one of the quarters she cannot leave it before visiting all the squares in this quarter that are not forbidden.
In the figure you can see two routes of a recursive tour of the ant on the board of size . Both routes begin in the start square . First of them is finished in the top periphery and the second in the left periphery of the board.
Write a program that:
Note: each of the four squares in the corners of the board belongs to two peripheries.
In the first line of the standard input there is one positive integer , . In the second line there is the number of forbidden squares , .
In each of the following lines there is a pair of non-negative integers separated by a single space; and are coordinates of a forbidden square ( is the number of line and is the number of column). The lines and columns of the board are numbered from to . The top-left square has coordinates .
In each of four consecutive lines of the standard output there should be written two non-negative integers separated by a single space. These integers should be coordinates (the number of line and column) of the end square of an appropriate tour. If such a square does not exists the word NIE (which means “no” in Polish) should be written. In the first line there should be the square of the tour ending on the top periphery, in the second — on the right periphery, in the third — on the bottom periphery and in the fourth line — on the left periphery.
For the input data:
3 17 2 0 1 0 3 0 6 0 7 0 6 1 7 1 6 2 7 2 6 3 7 3 0 2 0 3 0 6 0 7 1 6 1 7
the correct result is:
0 4 NIE NIE 4 0
Task author: Wojciech Rytter.